package com.conv.shop.business.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import com.conv.shop.business.domain.Product;
import com.conv.shop.business.domain.ProductExample;
import com.conv.shop.business.domain.StoreUser;
import com.conv.shop.business.domain.StoreUserExample;
import com.conv.shop.business.mapper.ProductMapper;
import com.conv.shop.business.mapper.StoreUserMapper;
import com.conv.shop.business.req.ProductReq;
import com.conv.shop.business.req.ProductSearchByNameReq;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductService {
    @Resource
    private ProductMapper productMapper;

    @Resource
    private StoreUserMapper storeUserMapper;

    /**
     * 根据名字查询单个商品
     * @param name
     * @return Product
     */

    public Product getProductByName(String name){
        ProductExample example = new ProductExample();
        example.createCriteria().andProductNameEqualTo(name);
        List<Product> list = productMapper.selectByExample(example);
        return list.get(0);
    }

    /**
     *  根据productId查询商品
     * @param product
     * @return Product
     */
    public Product getProductById(Product product){
       return productMapper.selectByPrimaryKey(product.getProductId());
    }

    /**
     *  查询商品详细信息
     * @param id
     * @return Product
     */
    public Product getProductByCode(Integer id){
        ProductExample example = new ProductExample();
        example.createCriteria().andProductIdEqualTo(id);
        List<Product> list = productMapper.selectByExample(example);
        return list.get(0);
    }

    /**
     * 检索所有商品，可模糊查询
      * @param pageNum
     * @param pageSize
     * @param productName
     * @return List<Product>
     */
    public List<Product> getProductList(int pageNum, int pageSize, String productName){
        return productMapper.getProductList("%"+productName+"%",pageNum-1,pageSize);

    }

    /**
     * 模糊查询商品
     * @param req
     * @return List<Product>
     */
    public List<Product> searchProduct(ProductSearchByNameReq req){
        return productMapper.getProductByNameLike("%"+req.getProductName()+"%");
    }

    /**
     * 商品分类查询
     * @param categoryId
     * @return List<Product>
     */
    public List<Product> getProductListbyCategory(Integer categoryId){
        ProductExample example = new ProductExample();
        example.createCriteria().andCategoryIdEqualTo(categoryId);
        List<Product> list = productMapper.selectByExample(example);
        return list;
    }

    /**
     * 添加商品
      * @param req
     * @return boolean
     */
    public boolean save(ProductReq req, Integer consumerId){
        StoreUserExample example = new StoreUserExample();
        example.createCriteria().andConsumerIdEqualTo(consumerId);
        List<StoreUser> storeUsers = storeUserMapper.selectByExample(example);
        if (storeUsers.isEmpty()){
            return false;
        }
        DateTime now = DateTime.now();
        Product product = BeanUtil.copyProperties(req, Product.class);
        product.setProductName(req.getProductName());
        product.setCategoryId(req.getCategoryId());
        product.setPrice(req.getPrice());
        product.setStockQuantity(req.getStockQuantity());
        product.setDescription(req.getDescription());
        product.setImageUrl(req.getImageUrl());
        product.setCreationTime(now);
        product.setLastUpdateTime(now);
        product.setIsOnSale(req.getIsOnSale());
        product.setBrand(req.getBrand());
        product.setStoreId(storeUsers.get(0).getStoreId());
        int i = productMapper.insert(product);
        if (i != 0){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 更新商品
      * @param id
     * @param req
     * @return boolean
     */
    public boolean update(int id,ProductReq req){
        DateTime now = DateTime.now();
        Product product = new Product();
        product.setProductName(req.getProductName());
        product.setCategoryId(req.getCategoryId());
        product.setPrice(req.getPrice());
        product.setStockQuantity(req.getStockQuantity());
        product.setDescription(req.getDescription());
        product.setImageUrl(req.getImageUrl());
        product.setLastUpdateTime(now);
        product.setIsOnSale(req.getIsOnSale());
        product.setBrand(req.getBrand());
        product.setStoreId(req.getStoreId());
        product.setProductId(id);
        return productMapper.updateByPrimaryKeySelective(product) !=0 ;
    }

    /**
     * 下架商品
     * @param id
     * @return boolean
     */
    public boolean unpublishProduct(int id){
        Product product = new Product();
        product.setProductId(id);
        product.setIsOnSale(0);
        int i = productMapper.updateByPrimaryKeySelective(product);
        if (i != 0){
            return true;
        }else {
            return false;
        }
    }

    /**
     * 库存管理
     * @param id
     * @param num
     * @param option
     * @return boolean
     */
    public boolean manageInventory(int id,int num,int option){
        Product product = new Product();
       ProductExample example = new ProductExample();
       example.createCriteria().andProductIdEqualTo(id);
       try {
           List<Product> list = productMapper.selectByExample(example);
           if (list.size() == 0){
               System.out.println("错误");
               return false;
           }else {
               int quantity = list.get(0).getStockQuantity();
               product.setProductId(id);
               if (option == 0) {
                   product.setStockQuantity(quantity);
               } else if (option == 1) {
                   if(quantity >= num){
                       product.setStockQuantity(quantity - num);
                   }else{
                       System.out.println("错误");
                       return false;
                   }
               } else {
                   product.setStockQuantity(quantity + num);
               }
               int i = productMapper.updateByPrimaryKeySelective(product);
               return i != 0;
           }
       }catch (Exception e){
           e.printStackTrace();
           System.out.println("发生异常: " + e.getMessage());
           return false;
       }
    }

    /**
     * 根据店家查询商品
     * @param consumerId
     * @return List<Product>
     */
    public List<Product> getProductByStore(int consumerId){
        StoreUserExample example = new StoreUserExample();
        example.createCriteria().andConsumerIdEqualTo(consumerId);
        ProductExample Pexample = new ProductExample();
        List<StoreUser> storeUsers = storeUserMapper.selectByExample(example);
        if (storeUsers.isEmpty()){
            return null;
        }
        Pexample.createCriteria().andStoreIdEqualTo(storeUsers.get(0).getStoreId());
        List<Product> list = productMapper.selectByExample(Pexample);
        return list;
    }

    /**
     * 下架
     * @param id
     * @return
     */
    public boolean delete(int id){
        return productMapper.deleteByPrimaryKey(id) != 0;
    }
}
